package com.plum.admin.modules.common;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plum.admin.common.constant.Constant;
import com.plum.admin.common.util.R;
import com.plum.admin.common.util.RedisCache;
import com.plum.admin.modules.sys.entity.SysUser;
import com.plum.admin.modules.sys.service.ISysUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 子丶沫
 */
@RestController
@Api(tags = "短信API")
@RequestMapping("/sms")
public class SmsCodeController {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private RedisCache redisCache;
    @GetMapping("/sendSms/{mobile}")
    public R<Object> sendSmsCode(@PathVariable("mobile")String mobile){
        List<SysUser> list = userService.list(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getPhone, mobile));
        if(CollUtil.isEmpty(list)){
            return R.error("手机号码不存在");
        }
        String code=RandomUtil.randomNumbers(6);
        redisCache.setCacheObject(Constant.SMS_PRE+mobile,code,2L, TimeUnit.MINUTES);
        return R.ok(true);
    }

}
